<?php

namespace App\Http\Controllers\Message;

use App\Constants\ResponseArray;
use App\Http\Controllers\BaseController;
use App\Http\Services\Message\MessageService;
use Illuminate\Http\Request;

class MessageController extends BaseController
{

    public function chatRoomList(Request $request)
    {
        $data = $request->all();
        $rules = [
            'key_word' => 'nullable|string',
        ];
        $result = $this->validator($data, $rules, []);
        if ($result) {
            return ResponseArray::parameterError($result, $rules);
        }

        $userInfo = auth('a')->user();
        if (!$userInfo)
        {
            return ResponseArray::unauthenticatedError();
        }

        $res = MessageService::chatRoomList($request,$userInfo['member']['id']);

        return ResponseArray::response($res,'获取聊天列表成功.');
    }

    public function chatRoomMessageList(Request $request)
    {
        $data = $request->all();
        $rules = [
            'chat_id' => 'required_without:to_member_id|string',
            'to_member_id' => 'required_without:chat_id|string',
        ];
        $result = $this->validator($data, $rules, []);
        if ($result) {
            return ResponseArray::parameterError($result, $rules);
        }

        $userInfo = auth('a')->user();
        if (!$userInfo)
        {
            return ResponseArray::unauthenticatedError();
        }

        $res = MessageService::chatRoomMessageList($request,$userInfo['member']['id']);

        return ResponseArray::response($res,'获取聊天消息成功.');
    }

    public function sendMessage(Request $request)
    {
        $data = $request->all();
        //var_dump('$data',$data);
        $rules = [
            'to_member_id' => 'required',
            'message'      => 'required|string',
        ];
        $result = $this->validator($data, $rules, []);
        if ($result) {
            return ResponseArray::parameterError($result, $rules);
        }
        $userInfo = auth('a')->user();
        if (!$userInfo)
        {
            return ResponseArray::unauthenticatedError();
        }
        if ($data['to_member_id'] == $userInfo['member']['id'])
        {
            return ResponseArray::renderError('无法给自己发送消息.',false);
        }

        $res = MessageService::sendMessage($userInfo['member']['id'],$data['to_member_id'],$data['message']);

        if (!$res)
        {
            return ResponseArray::renderError('消息发送失败,请重新尝试.',false);
        }

        return ResponseArray::response(true,'消息发送成功.');
    }


    /**
     * 未读聊天数量
     * @param Request $request
     * @return array
     */
    public function unreadChatCount(Request $request)
    {
        $userInfo = auth('a')->user();
        if (!$userInfo)
        {
            return ResponseArray::unauthenticatedError();
        }

        $res = MessageService::unreadChatCount($userInfo['member']['id']);

        return ResponseArray::response($res,'获取未读统计成功.');
    }

    /**
     * 全部已读
     * @param Request $request
     * @return array
     */
    public function readAllChat(Request $request)
    {
        $userInfo = auth('a')->user();
        if (!$userInfo)
        {
            return ResponseArray::unauthenticatedError();
        }

        $res = MessageService::readAllChat($userInfo['member']['id']);

        return ResponseArray::response($res,'全部已读成功.');
    }

}
